home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Freeware / Griffith 0.9.8 / griffith-0.9.8-win32.exe / {app} / lib / test_movieplugins.py < prev    next >
Text File  |  2008-11-17  |  10KB  |  305 lines

  1. # -*- coding: UTF-8 -*-
  2.  
  3. __revision__ = '$Id: test_movieplugins.py 1042 2008-11-15 21:59:58Z mikej06 $'
  4.  
  5. # Copyright (c) 2006-2007
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Library General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  20.  
  21. # You may use and distribute this software under the terms of the
  22. # GNU General Public License, version 2 or later
  23.  
  24. #
  25. # The code within this file is only used to automatically test movie plugins
  26. # which support that.
  27. # The movie plugin which should be tested has to be added to the
  28. # PluginTester.test_plugins list and has to define to classes
  29. # SearchPluginTest and PluginTest
  30. # Both classes provide a member called test_configuration which is a
  31. # dict in both cases.
  32. #
  33. # SearchPluginTest.test_configuration:
  34. # dict { movie_id -> [ expected result count for original url, expected result count for translated url ] }
  35. #
  36. # PluginTest.test_configuration:
  37. # dict { movie_id -> dict { arribute -> value } }
  38. #
  39. # value: * True/False if attribute should only be tested for any value or nothing
  40. #        * or the expected value
  41. #
  42.  
  43. import gettext
  44. import sys
  45. import initialize
  46. import gdebug
  47. import gutils
  48. import config
  49. import os
  50. from time import sleep
  51. try:
  52.     import gtk
  53.     import gobject
  54. except:
  55.     pass
  56.     
  57. sys.path.append('plugins/movie')
  58.  
  59. #
  60. # test class for movie plugin classes Plugin and SearchPlugin
  61. # it simulates the resolving of movie data for configured movies and
  62. # compares the results with the expected once
  63. #
  64. class PluginTester:
  65.     test_plugins = [
  66.         'PluginMovieAmazon',
  67.         'PluginMovieFilmeVonAZ',
  68.         'PluginMovieIMDB-de',
  69.         'PluginMovieKinoDe',
  70.         'PluginMovieOFDb',
  71.         'PluginMovieZelluloid',
  72.     ]
  73.  
  74.     #
  75.     # simulates the search for a movie title and compares
  76.     # the count of results with the expected count
  77.     #
  78.     def test_search(self, plugin, title, cntOriginal, cntTranslated):
  79.         global debug, myconfig
  80.         result = True
  81.         plugin.debug = debug
  82.         plugin.config = myconfig
  83.         # plugin.translated_url_search
  84.         plugin.url = plugin.translated_url_search
  85.         if plugin.remove_accents:
  86.             plugin.title = gutils.remove_accents(title, 'utf-8')
  87.         else:
  88.             plugin.title = title
  89.         plugin.search_movies(None)
  90.         plugin.get_searches()
  91.         if not len(plugin.ids) - 1 == cntOriginal:    # first entry is always '' (???)
  92.             print "Title (Translated): %s - expected: %d - found: %d" % (title, cntOriginal, len(plugin.ids) - 1)
  93.             result = False
  94.         # plugin.original_url_search
  95.         plugin.url = plugin.original_url_search
  96.         if plugin.remove_accents:
  97.             plugin.title = gutils.remove_accents(title, 'utf-8')
  98.         else:
  99.             plugin.title = title
  100.         plugin.search_movies(None)
  101.         plugin.get_searches()
  102.         if not len(plugin.ids) - 1 == cntTranslated:    # first entry is always '' (???)
  103.             print "Title (Original): %s - expected: %d - found: %d" % (title, cntTranslated, len(plugin.ids) - 1)
  104.             result = False
  105.         return result
  106.  
  107.     #
  108.     # check every configured movie title
  109.     #
  110.     def do_test_searchplugin(self, plugin_name, domsgbox=True):
  111.         result = True
  112.         
  113.         plugin = __import__(plugin_name)
  114.         try:
  115.             pluginTestConfig = plugin.SearchPluginTest()
  116.         except:
  117.             print "Warning: SearchPlugin test could not be executed for %s because of missing configuration class SearchPluginTest." % plugin_name
  118.             pluginTestConfig = None
  119.         
  120.         if not pluginTestConfig == None:
  121.             for i in pluginTestConfig.test_configuration:
  122.                 searchPlugin = plugin.SearchPlugin()
  123.                 if not self.test_search(searchPlugin, i, pluginTestConfig.test_configuration[i][0], pluginTestConfig.test_configuration[i][1]):
  124.                     result = False
  125.                 sleep(1) # needed for amazon
  126.         
  127.         if domsgbox:
  128.             if not result:
  129.                 gutils.error(self, 'SearchPluginTest %s: Test NOT successful !' % plugin_name)
  130.             else:
  131.                 gutils.info(self, 'SearchPluginTest %s: Test successful !' % plugin_name)
  132.         
  133.         return result
  134.  
  135.     #
  136.     # simulates the resolving of movie data for configured movies and
  137.     # compares the results with the expected once
  138.     #
  139.     def test_one_movie(self, movieplugin, results_expected):
  140.         global debug, myconfig
  141.         result = True
  142.         self.movie = movieplugin
  143.         self.movie.parent_window = None
  144.         self.movie.locations = self.locations
  145.         self.movie.debug = debug
  146.         self.movie.config = myconfig
  147.  
  148.         fields_to_fetch = ['o_title', 'title', 'director', 'plot', 'cast', 'country', 'genre',
  149.                 'classification', 'studio', 'o_site', 'site', 'trailer', 'year',
  150.                 'notes', 'runtime', 'image', 'rating']
  151.  
  152.         self.movie.fields_to_fetch = fields_to_fetch
  153.     
  154.         self.movie.get_movie(None)
  155.         self.movie.parse_movie()
  156.  
  157.         results = {}
  158.         if 'year' in fields_to_fetch:
  159.             results['year'] = self.movie.year
  160.             fields_to_fetch.pop(fields_to_fetch.index('year'))
  161.         if 'runtime' in fields_to_fetch:
  162.             results['runtime'] = self.movie.runtime
  163.             fields_to_fetch.pop(fields_to_fetch.index('runtime'))
  164.         if 'cast' in fields_to_fetch:
  165.             results['cast'] = gutils.convert_entities(self.movie.cast)
  166.             fields_to_fetch.pop(fields_to_fetch.index('cast'))
  167.         if 'plot' in fields_to_fetch:
  168.             results['plot'] = gutils.convert_entities(self.movie.plot)
  169.             fields_to_fetch.pop(fields_to_fetch.index('plot'))
  170.         if 'notes' in fields_to_fetch:
  171.             results['notes'] = gutils.convert_entities(self.movie.notes)
  172.             fields_to_fetch.pop(fields_to_fetch.index('notes'))
  173.         if 'rating' in fields_to_fetch:
  174.             if self.movie.rating:
  175.                 results['rating'] = float(self.movie.rating)
  176.             fields_to_fetch.pop(fields_to_fetch.index('rating'))
  177.         # poster
  178.         if 'image' in fields_to_fetch:
  179.             if self.movie.image:
  180.                 results['image'] = self.movie.image
  181.             fields_to_fetch.pop(fields_to_fetch.index('image'))
  182.         # other fields
  183.         for i in fields_to_fetch:
  184.             results[i] = gutils.convert_entities(self.movie[i])
  185.             
  186.         # check the fields
  187.         for i in results_expected:
  188.             i_val = results_expected[i]
  189.             if isinstance(i_val, bool):
  190.                 if i_val:
  191.                     if not results.has_key(i) or len(results[i]) < 1:
  192.                         print "Test error: %s: Value expected but nothing returned.\nKey: %s" % (movieplugin.movie_id, i)
  193.                         result = False
  194.                 else:
  195.                     if results.has_key(i) and len(results[i]) > 0:
  196.                         print "Test error: %s: No value expected but something returned.\nKey: %s\nValue: %s" % (movieplugin.movie_id, i, results[i])
  197.                         result = False
  198.             else:
  199.                 if not results.has_key(i):
  200.                     print "Test error: %s: Value expected but nothing returned.\nKey: %s" % (movieplugin.movie_id, i)
  201.                     result = False
  202.                 else:
  203.                     if not results[i] == i_val:
  204.                         print "Test error: %s: Wrong value returned.\nKey: %s\nValue expected: %s\nValue returned: %s" % (movieplugin.movie_id, i, i_val, results[i])
  205.                         result = False
  206.         return result
  207.     
  208.     #
  209.     # check every configured movie
  210.     #
  211.     def do_test_plugin(self, plugin_name, domsgbox=True):
  212.         result = True
  213.         
  214.         plugin = __import__(plugin_name)
  215.         try:
  216.             pluginTestConfig = plugin.PluginTest()
  217.         except:
  218.             print "Warning: Plugin test could not be executed for %s because of missing configuration class PluginTest." % plugin_name
  219.             pluginTestConfig = None
  220.         
  221.         if not pluginTestConfig == None:
  222.             for i in pluginTestConfig.test_configuration:
  223.                 moviePlugin = plugin.Plugin(i)
  224.                 if not self.test_one_movie(moviePlugin, pluginTestConfig.test_configuration[i]):
  225.                     result = False
  226.                 sleep(1) # needed for amazon
  227.  
  228.         if domsgbox:
  229.             if not result:
  230.                 gutils.error(self, 'PluginTest %s: Test NOT successful !' % plugin_name)
  231.             else:
  232.                 gutils.info(self, 'PluginTest %s: Test successful !' % plugin_name)
  233.         
  234.         return result
  235.  
  236.     #
  237.     # main method
  238.     # iterates through all plugins which should be auto-tested
  239.     # and executes the Plugin and SearchPlugin test methods
  240.     #
  241.     def do_test(self, domsgbox=True):
  242.         global debug, myconfig
  243.         debug = self.debug = gdebug.GriffithDebug()
  244.         #self.debug.set_debug(True)
  245.         self._tmp_home = None
  246.         self._tmp_config = 'griffith.cfg'
  247.         initialize.locations(self)
  248.         gettext.install('griffith', self.locations['i18n'], unicode=1)
  249.         configFileName = os.path.join(self.locations['home'], self._tmp_config)
  250.         myconfig = self.config = config.Config(file=configFileName)
  251.         search_successful = ''
  252.         search_unsuccessful = ''
  253.         get_successful = ''
  254.         get_unsuccessful = ''
  255.         # test all plugins ?
  256.         test_all = True
  257.         dialog = gtk.MessageDialog(None,
  258.             gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
  259.             gtk.MESSAGE_QUESTION, gtk.BUTTONS_NONE, 'Test all plugins ?')
  260.         dialog.add_buttons(gtk.STOCK_YES, gtk.RESPONSE_YES,
  261.             gtk.STOCK_NO, gtk.RESPONSE_NO)
  262.         dialog.set_default_response(gtk.RESPONSE_NO)
  263.         dialog.set_skip_taskbar_hint(False)
  264.         response = dialog.run()
  265.         dialog.destroy()
  266.         if not response == gtk.RESPONSE_YES:
  267.             test_all = False
  268.         # iterate through all testable plugins
  269.         for i in self.test_plugins:
  270.             if domsgbox and test_all == False:
  271.                 # ask for test of that specific plugin
  272.                 dialog = gtk.MessageDialog(None,
  273.                     gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
  274.                     gtk.MESSAGE_QUESTION, gtk.BUTTONS_NONE, 'Test plugin %s ?' %i)
  275.                 dialog.add_buttons(gtk.STOCK_YES, gtk.RESPONSE_YES,
  276.                     gtk.STOCK_NO, gtk.RESPONSE_NO)
  277.                 dialog.set_default_response(gtk.RESPONSE_NO)
  278.                 dialog.set_skip_taskbar_hint(False)
  279.                 response = dialog.run()
  280.                 dialog.destroy()
  281.                 if not response == gtk.RESPONSE_YES:
  282.                     continue
  283.             plugin = __import__(i)
  284.             # search test
  285.             if self.do_test_searchplugin(i, False):
  286.                 search_successful = search_successful + i + ', '
  287.             else:
  288.                 search_unsuccessful = search_unsuccessful + i + ', '
  289.             # movie test
  290.             if self.do_test_plugin(i, False):
  291.                 get_successful = get_successful + i + ', '
  292.             else:
  293.                 get_unsuccessful = get_unsuccessful + i + ', '
  294.         if domsgbox:
  295.             gutils.info(self, 'SearchPluginTests\n  Success: %s\n  Failed: %s\n\nPluginTests\n  Success: %s\n  Failed: %s' % (search_successful, search_unsuccessful, get_successful, get_unsuccessful))
  296.  
  297. #
  298. # Start the tests
  299. #
  300. gtk.gdk.threads_init()
  301. gtk.gdk.threads_enter()
  302. PluginTester().do_test()
  303. #gtk.main()
  304. gtk.gdk.threads_leave()
  305.